package org.linlinjava.litemall.admin.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.constraints.NotNull;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import org.linlinjava.litemall.admin.beans.Constants;
import org.linlinjava.litemall.admin.beans.annotation.LogAnno;
import org.linlinjava.litemall.admin.beans.annotation.LoginAdminShopId;
import org.linlinjava.litemall.admin.service.AdService;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.validator.Order;
import org.linlinjava.litemall.core.validator.Sort;
import org.linlinjava.litemall.db.domain.LitemallAd;
import org.linlinjava.litemall.db.service.LitemallAdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 管理广告控制器
 *
 * @author yliyun
 * @date 2022/09/14
 */
@RestController
@RequestMapping("/admin/ad")
@Validated
public class AdminAdController {

    /**
     * litemall广告服务
     */
    @Autowired
    private LitemallAdService litemallAdService;
    /**
     * 广告服务
     */
    @Autowired
    private AdService adService;

    /**
     * 列表
     *
     * @param name    名字
     * @param content 内容
     * @param page    页面
     * @param limit   限制
     * @param sort    排序
     * @param order   订单
     * @return {@link Object}
     */
    @GetMapping("/list")
    @LogAnno
    public Object list(String name, String content,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @Sort @RequestParam(defaultValue = "add_time") String sort,
                       @Order @RequestParam(defaultValue = "desc") String order) {
        return ResponseUtil.okPage(litemallAdService.querySelective(name, content, page, limit, sort, order));
    }

    /**
     * 验证
     *
     * @param ad 广告
     * @return {@link Object}
     */
    private Object validate(LitemallAd ad) {
        String name = ad.getName();
        if (StringUtils.isEmpty(name)) {
            return ResponseUtil.badArgument();
        }
        String content = ad.getContent();
        if (StringUtils.isEmpty(content)) {
            return ResponseUtil.badArgument();
        }
        return null;
    }

    /**
     * 创建
     *
     * @param ad     广告
     * @param shopId 商店id
     * @return {@link Object}
     */
    @PostMapping("/create")
    @LogAnno
    public Object create(@RequestBody LitemallAd ad, @LoginAdminShopId Integer shopId) {
        Object error = validate(ad);
        if (error != null) {
            return error;
        }
        if(null != shopId){
            ad.setType(Constants.AD_TYPE_SHOP);
        }
        litemallAdService.add(ad);
        return ResponseUtil.ok(ad);
    }

    /**
     * 广告详情 通过ID
     *
     * @param id id
     * @return {@link Object}
     */
    @GetMapping("/read")
    @LogAnno
    public Object read(@NotNull @RequestParam(value = "id") Integer id) {
        LitemallAd ad = litemallAdService.findById(id);
        return ResponseUtil.ok(ad);
    }

    /**
     * 更新
     *
     * @param ad     广告
     * @param shopId 商店id
     * @return {@link Object}
     */
    @PostMapping("/update")
    @LogAnno
    public Object update(@RequestBody LitemallAd ad, @LoginAdminShopId Integer shopId) {
        Object error = validate(ad);
        if (error != null) {
            return error;
        }
        if(null != shopId){
            ad.setType(Constants.AD_TYPE_SHOP);
        }
        return adService.update(ad, shopId);
    }

    /**
     * 删除
     *
     * @param ad 广告
     * @return {@link Object}
     */
    @PostMapping("/delete")
    @LogAnno
    public Object delete(@RequestBody LitemallAd ad) {
        Integer id = ad.getId();
        if (id == null) {
            return ResponseUtil.badArgument();
        }
        litemallAdService.deleteById(id);
        return ResponseUtil.ok();
    }

    @GetMapping("/error")
    @LogAnno
    public Object error(){
        //Assert.isTrue(false,"{}",JSON.toJSONString(ResponseUtil.fail(22,"地方大是大非上地方就是奶粉开始")));
        throw new RuntimeException("测试报错打印");
        //return null;
    }

}
